package it.polimi.traveldream.web.beans.employee;

import it.polimi.traveldream.ejbclient.dtos.ExcursionDTO;
import it.polimi.traveldream.ejbclient.eaos.BaseProductServiceEAO;
import it.polimi.traveldream.utils.exceptions.NotCoherentException;
import it.polimi.traveldream.web.beans.shared.FacesContextHandler;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name="addExcursion")
@RequestScoped
public class AddExcursionBean {
	@EJB
	private BaseProductServiceEAO bService;
	private ExcursionDTO excursion;
	
	public AddExcursionBean() {
	}
	
	@PostConstruct
	public void init() {
		excursion = new ExcursionDTO();
		excursion.setMaxAvailability(1);
	}
	
	public ExcursionDTO getExcursion() {
		return excursion;
	}

	public void setExcursion(ExcursionDTO excursion) {
		this.excursion = excursion;
	}

	public String addExcursion() {
		try {
			bService.addProduct(excursion);
			return "index";
		}  catch (NotCoherentException e) {
			FacesContextHandler.produceFacesMessage(null,FacesContext.getCurrentInstance(),
					FacesMessage.SEVERITY_ERROR, "The composition of the product is not coherent. Please check its fields.");
			return "addExcursion";
		}
	}
	
}
